package com.ihd.ihardware.view.enter.viewModel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.support.annotation.NonNull;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ItemHomeDeviceBinding;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.pojo.HomeDevice;
import com.ihd.ihardware.pojo.UpdateRes;
import com.ihd.ihardware.view.enter.model.HomeRepository;

import java.util.List;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;

public class HomeModel extends AndroidViewModel {

    private HomeRepository mHomeRepository;

    public HomeModel(@NonNull Application application) {
        super(application);
    }

    private ObservableArrayList<HomeDevice> mDV0 = new ObservableArrayList<>();
    public CommonAdapter mDV0Adapter = new CommonAdapter<HomeDevice, ItemHomeDeviceBinding>(R.layout.item_home_device, mDV0, null) {
        @Override
        public void convert(ItemHomeDeviceBinding mDataBinding, HomeDevice hd, int position) {
            mDataBinding.concernItemLL.setBackgroundResource(hd.getImg());
            mDataBinding.name.setText(hd.getName());
            mDataBinding.hint.setText(hd.getHint());
            mDataBinding.icon.setImageResource(hd.getType());
            if (mDV0.get(position).getStatus() == 0) {
                mDataBinding.status.setText("去绑定");
                mDataBinding.status.setBackgroundResource(R.drawable.corners_line_w_4);
                mDataBinding.status.setTextColor(mDataBinding.status.getContext().getResources().getColor(R.color.C_FFFFFF));
            } else if (mDV0.get(position).getStatus() == 1) {
                mDataBinding.status.setText("已绑定");
                mDataBinding.status.setBackgroundResource(R.drawable.corners_w_33);
                mDataBinding.status.setTextColor(mDataBinding.status.getContext().getResources().getColor(R.color.C_FFFFFF));
            } else {
                mDataBinding.status.setText("尽请期待");
                mDataBinding.status.setBackgroundResource(R.drawable.corners_b_33);
                mDataBinding.status.setTextColor(mDataBinding.status.getContext().getResources().getColor(R.color.C_4cFFFFFF));
            }
        }
    };

    public void getLifeHealthList(DeviceListRes.DataBean.ListBean tzcddl) {
        if (mHomeRepository == null)
            mHomeRepository = new HomeRepository();
        mHomeRepository.getLifeHealthList(mDV0, tzcddl);
    }

    private ObservableArrayList<HomeDevice> mDV1 = new ObservableArrayList<>();
    public CommonAdapter mDV1Adapter = new CommonAdapter<HomeDevice, ItemHomeDeviceBinding>(R.layout.item_home_device, mDV1, null) {
        @Override
        public void convert(ItemHomeDeviceBinding mDataBinding, HomeDevice hd, int position) {
            mDataBinding.concernItemLL.setBackgroundResource(hd.getImg());
            mDataBinding.name.setText(hd.getName());
            mDataBinding.hint.setText(hd.getHint());
            mDataBinding.icon.setImageResource(hd.getType());
            if (mDV1.get(position).getStatus() == 0) {
                mDataBinding.status.setText("去绑定");
                mDataBinding.status.setBackgroundResource(R.drawable.corners_line_w_4);
                mDataBinding.status.setTextColor(mDataBinding.status.getContext().getResources().getColor(R.color.C_FFFFFF));
            } else if (mDV1.get(position).getStatus() == 1) {
                mDataBinding.status.setText("已绑定");
                mDataBinding.status.setBackgroundResource(R.drawable.corners_w_33);
                mDataBinding.status.setTextColor(mDataBinding.status.getContext().getResources().getColor(R.color.C_FFFFFF));
            } else {
                mDataBinding.status.setText("尽请期待");
                mDataBinding.status.setBackgroundResource(R.drawable.corners_b_33);
                mDataBinding.status.setTextColor(mDataBinding.status.getContext().getResources().getColor(R.color.C_4cFFFFFF));
            }
        }
    };

    public void getHealthWorkList(DeviceListRes.DataBean.ListBean hbcddl) {
        if (mHomeRepository == null)
            mHomeRepository = new HomeRepository();
        mHomeRepository.getHealthWorkList(mDV1, hbcddl);
    }

    private ObservableArrayList<HomeDevice> mDV2 = new ObservableArrayList<>();
    public CommonAdapter mDV2Adapter = new CommonAdapter<HomeDevice, ItemHomeDeviceBinding>(R.layout.item_home_device, mDV2, null) {
        @Override
        public void convert(ItemHomeDeviceBinding mDataBinding, HomeDevice hd, int position) {
            mDataBinding.concernItemLL.setBackgroundResource(hd.getImg());
            mDataBinding.name.setText(hd.getName());
            mDataBinding.hint.setText(hd.getHint());
            mDataBinding.icon.setImageResource(hd.getType());
            if (mDV2.get(position).getStatus() == 0) {
                mDataBinding.status.setText("去绑定");
                mDataBinding.status.setBackgroundResource(R.drawable.corners_line_w_4);
                mDataBinding.status.setTextColor(mDataBinding.status.getContext().getResources().getColor(R.color.C_FFFFFF));
            } else if (mDV2.get(position).getStatus() == 1) {
                mDataBinding.status.setText("已绑定");
                mDataBinding.status.setBackgroundResource(R.drawable.corners_w_33);
                mDataBinding.status.setTextColor(mDataBinding.status.getContext().getResources().getColor(R.color.C_FFFFFF));
            } else {
                mDataBinding.status.setText("尽请期待");
                mDataBinding.status.setBackgroundResource(R.drawable.corners_b_33);
                mDataBinding.status.setTextColor(mDataBinding.status.getContext().getResources().getColor(R.color.C_4cFFFFFF));
            }
        }
    };

    public void getLifeUseList(List<DeviceListRes.DataBean.ListBean> lysddl) {
        if (mHomeRepository == null)
            mHomeRepository = new HomeRepository();
        mHomeRepository.getLifeUseList(mDV2, lysddl);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != mHomeRepository) {
            mHomeRepository.onDestroy();
            mHomeRepository = null;
        }
    }
}
